import Vue from 'vue'
import VueRouter from 'vue-router'

const Home = () => import('../components/Home.vue')
const Welcome = () => import('../components/Welcome.vue')
const Login = () => import('../components/Login.vue')
const Dept = () => import('../components/user/Dept.vue')
const User = () => import('../components/user/User.vue')
const Stu = () => import('../components/person/Student.vue')
const Tec = () => import('../components/person/Teacher.vue')
const Cur = () => import('../components/curriculum/Curriculum.vue')
const ChoseCur = () => import('../components/curriculum/ChoseCur.vue')
const Notice = () => import('../components/notice/Notice.vue')

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/dept', component: Dept },
      { path: '/user', component: User },
      { path: '/stu', component: Stu },
      { path: '/tec', component: Tec },
      { path: '/cur', component: Cur },
      { path: '/ccur', component: ChoseCur },
      { path: '/notice', component: Notice }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
